<?php
session_start();
if($_SESSION['user']=="") {
    ?>
    <script>
        alert('当前未登录！');
        window.location.href='../login.html';
    </script>
    <?php
}
//连接数据库
try {
    function success($res){
        $result=[
            'error' => 0,
            'message' => $res
        ];
        return json_encode($result);
    }
    function error($mag){
        $data = [
            'error' => 1,
            'message' => $mag
        ];
        return json_encode($data);
    }

    //连接数据库
    $db = new PDO('mysql:dbname=liaotian;host=106.14.7.147','root','123456');

    $queryAll = $db->prepare('select username from user_room where roomId=:roomId');
    $data = [
        ':roomId' => $_GET['roomId']
    ];
    if ($queryAll->execute($data) === false) {
        print_r($queryAll->errorInfo());
    }
    $reQueryAll = $queryAll->fetch();
    $search = $db->prepare('select user.userImg,user_room.username,user.nicheng from user_room 
                                        LEFT JOIN user ON  user.username=user_room.username 
                                        WHERE user_room.roomId=:roomId and user_room.status=:status');
    $data = [
        ':roomId' => $_GET['roomId'],
        ':status' => 1
    ];
    if ($search->execute($data) === false) {
        print_r($search->errorInfo());
    }
    $re = $search->fetchAll();
    echo success($re);
} catch (PDOException $e) {
    echo 'Connection failed';
}
